home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / graph_layout / graph.hxx < prev    next >
Encoding:
Text File  |  1995-02-06  |  6.3 KB  |  163 lines

  1. /************************************************************************
  2. **    TEST FILE FOR graph (Dynamic Layout Alg)
  3. **
  4. **    HEADER - GRAPH DATA STRUCTURE MANIPULATION
  5. **
  6. ** Author: dr. Szirmay-Kalos Laszlo (szirmay@fsz.bme.hu)
  7. **       Technical University of Budapest, Hungary
  8. *************************************************************************/
  9. #ifdef MSWINDOWS
  10. #include "mswindow.hxx"
  11. #include "vector.hxx"
  12. #include "defines.h"
  13. #else
  14. #include "window.hxx"
  15. #include "vector.hxx"
  16. #include "defines.h"
  17. #endif
  18.  
  19. typedef char TYPE;
  20.  
  21. /************************************************************************/
  22. class Node {
  23. /************************************************************************/
  24.     char   name[MAXNAME + 1];    // node name
  25.     TYPE   type;        // fixed or movable
  26.     vector pos;            // actual position
  27.     vector speed;        // speed
  28.     vector force;        // driving force to this node
  29. public:
  30.        Node( pchar, TYPE );         // constructor
  31.  
  32.     vector& Position( void )         { return pos;        }
  33.     vector& Speed( void )         { return speed;        }
  34.     vector& Force( void )         { return force;        }
  35.  
  36.     void   AddForce( vector& f )     { force += f;        }
  37.  
  38.     pchar  GetName( void )         { return name;        }
  39.     TYPE   GetType( void )         { return type;        }
  40. };
  41.  
  42. /************************************************************************/
  43. class Relation {
  44. /************************************************************************/
  45.     char     name[MAXNAME + 1];       // relation name
  46.     double   intensity;           // relation intensity
  47.     Node   * relation_to;       // related node
  48. public:
  49.          Relation( pchar , Node *, double );
  50.     void     SetRelation( pchar, double );
  51.     pchar    GetName( void )         { return name;         }
  52.     double   GetRelation( void )     { return intensity;     }
  53.     Node *   GetOtherNode( void )     { return relation_to;     }
  54.  
  55. };
  56.  
  57. /************************************************************************/
  58. class RelationElem : public Relation {
  59. /************************************************************************/
  60.     RelationElem  *    next_relation;              // next on the list
  61. public:
  62.     RelationElem( pchar name, Node * p, double r );
  63.     void       SetNext( RelationElem * rn ) { next_relation = rn;    }
  64.     RelationElem * GetNext( void )        { return next_relation; }
  65. };
  66.  
  67. /************************************************************************/
  68. class NodeElem : public Node {
  69. /************************************************************************/
  70.     int            ser_num;        // serial number in list
  71.     NodeElem     *  next_node;        // pointer to next node
  72.     RelationElem *  relation;        // first relation of this node
  73. public:
  74.             NodeElem(pchar, TYPE);
  75.     void        SetNext( NodeElem *p )        { next_node = p;         }
  76.     void        SetRelation( RelationElem *p )  { relation = p;         }
  77.     void        SetSerNum( int sernum )        { ser_num = sernum;         }
  78.     NodeElem     *  GetNext( void )            { return next_node;         }
  79.     RelationElem *  GetRelation( void )            { return relation;         }
  80.     int            GetSerNum( void )            { return ser_num;         }
  81. };
  82.  
  83.  
  84. /************************************************************************/
  85. class Graph {
  86. /************************************************************************/
  87.     int           nfixnode;         // number of fix nodes
  88.     int           nmovnode;         // number of movable nodes
  89.     NodeElem *       currnode;         // current node
  90.     NodeElem *       relatenode;         // actual relation of curr
  91.     NodeElem *       start_node;         // start of list
  92.     NodeElem *       last_node;         // end of list
  93.     RelationElem * currelation;         // relation of nodes list
  94.     RelationElem * prevrelation;     // previous to currelation
  95.  
  96.     void     SwapRelation( void );      // swap currnode and relatenode
  97.                       // if currnode is further in the
  98.                       // list
  99. public:
  100.          Graph( void );
  101.  
  102.     void     SetNodePos( vector );      // sets position of currnode
  103.     void     SetRelation( double );      // sets intensity of currelation
  104.     NodeElem *     GetNode( void )      { return currnode;           }
  105.     NodeElem *     GetRelateNode( void )      { return relatenode;           }
  106.     double     GetRelation( void );      // get intensity of currelation
  107.     pchar     GetRelationName( void ); // get name of currelation
  108.     BOOL     AddNode( pchar, TYPE );  // add new node to the list
  109.     void     AddRelation( pchar, double ); // add new relation
  110.     BOOL     SearchNode( pchar );      // search node by name
  111.     BOOL     RelSearchNode( pchar );
  112.     int         SearchRelation( void );  // search relation of currnode and relatenode
  113.  
  114.     BOOL     SaveNodes( pchar );      // save to a file
  115.     void     RestoreNodes( pchar );      // restore from file
  116.  
  117.     int         Placement( void );      // place nodes step-by-step
  118.     void     RandomArrange( void );      // arrange nodes randomly
  119.     int         DynamicLayout( int );      // dynamic layout algorithm
  120.  
  121.     BOOL     FirstNode( void );      // select first node on the list
  122.     BOOL     FirstMoveNode( void );      // select first moveable node
  123.     BOOL     NextNode( int max = ALL_NODES ); // select next to currnode
  124.     BOOL     FirstRelation( void );      // select first relation of currnode
  125.     BOOL     NextRelation( void );      // select next relation
  126.  
  127. };
  128.  
  129. /************************************************************************/
  130. class ObjectSpace : public Graph {
  131. /************************************************************************/
  132.     double         scale_x;        // scale of window->viewport transform
  133.     double         scale_y;
  134.     RectAngle         vwindow;        // object space window
  135.     RectAngle         viewport;        // viewport
  136.     void         SetScale( void );    // calculate scale from vwindow and viewport
  137. public:
  138.        ObjectSpace( void );
  139.  
  140.     void   SetViewPort( RectAngle );
  141.     void   SetWindow( RectAngle     );
  142.  
  143.     Point  ScreenPos( NodeElem * );    // get screen coordinates of node
  144.     Point  ScreenPos( vector );           // window -> viewport transform
  145.     Point  ScreenPos( void )          { return ScreenPos( GetNode( ) );       }
  146.     Point  RelScreenPos( void )          { return ScreenPos( GetRelateNode() );}
  147. };
  148.  
  149.  
  150. /************************************************************************/
  151. class GraphWindow : public AppWindow {
  152. /************************************************************************/
  153.     ObjectSpace graph;
  154.  
  155.     void    ExposeAll( ExposeEvent *  );
  156.     void    KeyPressed( KeyEvent * );
  157.     void    ShowNode( void );
  158.     void    ShowRelation( void );
  159.  
  160. public:
  161.         GraphWindow(int argc, char * argv[] );
  162. };
  163.